package com.jucagal.kiosco.windows;

import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;

import javax.imageio.ImageIO;
import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;

import net.sourceforge.jdatepicker.impl.JDatePanelImpl;
import net.sourceforge.jdatepicker.impl.JDatePickerImpl;
import net.sourceforge.jdatepicker.impl.UtilDateModel;

import com.jucagal.kiosco.sql.Code;
import com.jucagal.kiosco.sql.SqlConnection;
import com.jucagal.kiosco.table.ExcelExporter;

public class Start extends JFrame implements ActionListener {
	private JButton buttonSearch, buttonExport;
	private JLabel labelTitle, labelFrom, labelTo, labelDocument, labelTicket;
	private JTextField textDocument, textTicket;
	private static Start instance;
	private String findBy, codeToFind;
	private String codigoFrom, codigoTo;
	private JLabel personNameLabel, pictureLabel, barcodeLabel, ticketLabel,
	serialLabel, deviceLabel, trademarckLabel, personLabel,
	serialNameLabel, deviceNameLabel, trademarckNameLabel;
	private JPanel queryArea;
	private JDatePickerImpl datePickerTo, datePickerFrom;
	private JTable tableExcel;
//	private  String filename="c:\\Reports\\";
	private String filename, choosertitle;
	private JFileChooser chooser;
	
	public static Start getInstance() {
		if (instance == null) {
			instance = new Start();
		}
		return instance;
	}
	
	private Start() {
	    setLayout(null);
	    
	    labelTitle=new JLabel("INFORME DE REGISTROS");
	    labelTitle.setBounds(400,30,500,30);
	    labelTitle.setFont(new Font("Arial", Font.PLAIN, 25));
	    add(labelTitle);
	    
	    JPanel radioPanel = new JPanel(new GridLayout(0, 1));
	    radioPanel.setBounds(50,100,240,200);
	    radioPanel.setFont(new Font("Arial", Font.PLAIN, 20));
	    add(radioPanel);
	    
	    ButtonGroup group = new ButtonGroup();
	
	    JRadioButton byDate = new JRadioButton("Informe por Fecha");
	    byDate.setFont(new Font("Arial", Font.PLAIN, 20));
	    byDate.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
            	ocultAll();
            	findBy = "date";
            	ocultAll();
            	labelFrom.setVisible(true);
            	datePickerFrom.setVisible(true);
                labelTo.setVisible(true);
                datePickerTo.setVisible(true);
            }
        });
	    radioPanel.add(byDate);	
	    group.add(byDate);
	    
        JRadioButton byDocument = new JRadioButton("Informe por Documento");
        byDocument.setFont(new Font("Arial", Font.PLAIN, 20));
        byDocument.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
            	ocultAll();
            	findBy = "document";
            	ocultAll();
                labelDocument.setVisible(true);
                textDocument.setVisible(true);
            }
        });
	    radioPanel.add(byDocument);
	    group.add(byDocument);
	    
        JRadioButton byTicket = new JRadioButton("Buscar Imagen");
        byTicket.setFont(new Font("Arial", Font.PLAIN, 20));
        byTicket.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
            	ocultAll();
            	findBy = "ticket";
            	ocultAll();
                labelTicket.setVisible(true);
                textTicket.setVisible(true);
            }
        });
	    radioPanel.add(byTicket);
	    group.add(byTicket);
	    
	    labelFrom=new JLabel("Desde:");
	    labelFrom.setBounds(400,130,300,30);
	    labelFrom.setFont(new Font("Arial", Font.PLAIN, 20));
	    labelFrom.setVisible(false);
	    add(labelFrom);
		
		UtilDateModel model = new UtilDateModel();
		JDatePanelImpl datePanel = new JDatePanelImpl(model);
		datePickerFrom = new JDatePickerImpl(datePanel);
		datePickerFrom.setBounds(400,180,150,30);
		datePickerFrom.setFont(new Font("Arial", Font.PLAIN, 20));
		datePickerFrom.setVisible(false);
		add(datePickerFrom);
	    
	    labelTo=new JLabel("Hasta:");
	    labelTo.setBounds(600,130,300,30);
	    labelTo.setFont(new Font("Arial", Font.PLAIN, 20));
	    labelTo.setVisible(false);
	    add(labelTo);
	    
	    UtilDateModel model2 = new UtilDateModel();
		JDatePanelImpl datePanel2 = new JDatePanelImpl(model2);
		datePickerTo = new JDatePickerImpl(datePanel2);
		datePickerTo.setBounds(600,180,150,30);
		datePickerTo.setFont(new Font("Arial", Font.PLAIN, 20));
		datePickerTo.setVisible(false);
		add(datePickerTo);
	    
	    labelDocument=new JLabel("Documento:");
	    labelDocument.setBounds(400,130,300,30);
	    labelDocument.setFont(new Font("Arial", Font.PLAIN, 20));
	    labelDocument.setVisible(false);
	    add(labelDocument);
	    
	    textDocument = new JTextField(30);
	    textDocument.setBounds(400,180,300,30);
	    textDocument.setFont(new Font("Arial", Font.PLAIN, 20));
	    textDocument.setVisible(false);
	    add(textDocument);
	    
	    labelTicket=new JLabel("Numero de Ticket:");
	    labelTicket.setBounds(400,130,300,30);
	    labelTicket.setFont(new Font("Arial", Font.PLAIN, 20));
	    labelTicket.setVisible(false);
	    add(labelTicket);
	    
	    textTicket = new JTextField(30);
	    textTicket.setBounds(400,180,300,30);
	    textTicket.setFont(new Font("Arial", Font.PLAIN, 20));
	    textTicket.setVisible(false);
	    add(textTicket);
		
	    buttonSearch=new JButton("Buscar");
	    buttonSearch.setBounds(780,120,150,35);;
	    buttonSearch.setFont(new Font("Arial", Font.PLAIN, 25));
	    add(buttonSearch);
	    buttonSearch.addActionListener(this);
	    buttonSearch.setFocusable(false);
	    
	    buttonExport=new JButton("Exportar");
	    buttonExport.setBounds(780,175,150,35);;
	    buttonExport.setFont(new Font("Arial", Font.PLAIN, 25));
	    add(buttonExport);
	    buttonExport.addActionListener(this);
	    buttonExport.setFocusable(false);
	    buttonExport.setVisible(false);
	    
	    // Buscar imagen
	    
	    personLabel =new JLabel("Nombre:");
	    personLabel.setBounds(20, 400, 80, 20);
	    personLabel.setFont(new Font("Arial", Font.PLAIN, 20));
		add(personLabel);
		personLabel.setVisible(false);
		
	    personNameLabel =new JLabel("");
		personNameLabel.setBounds(120, 400, 260, 20);
		personNameLabel.setFont(new Font("Arial", Font.PLAIN, 20));
		add(personNameLabel);
		personNameLabel.setVisible(false);
		
		pictureLabel = new JLabel(new ImageIcon());
		pictureLabel.setBounds(380, 220, 640, 480);
		add(pictureLabel);
		pictureLabel.setVisible(false);
		
		ticketLabel =new JLabel("Ticket:");
		ticketLabel.setBounds(20, 450, 100, 20);
		ticketLabel.setFont(new Font("Arial", Font.PLAIN, 20));
		add(ticketLabel);
		ticketLabel.setVisible(false);
		
		barcodeLabel =new JLabel("");
		barcodeLabel.setBounds(180, 450, 250, 20);
		barcodeLabel.setFont(new Font("Arial", Font.PLAIN, 20));
		add(barcodeLabel);
		barcodeLabel.setVisible(false);
		
		deviceNameLabel = new JLabel("Dispositivo:");
		deviceNameLabel.setBounds(20, 500, 100, 20);
		deviceNameLabel.setFont(new Font("Arial", Font.PLAIN, 20));
		add(deviceNameLabel);
		deviceNameLabel.setVisible(false);
		
		deviceLabel = new JLabel();
		deviceLabel.setBounds(180, 500, 250, 20);
		deviceLabel.setFont(new Font("Arial", Font.PLAIN, 20));
		add(deviceLabel);
		deviceLabel.setVisible(false);
		
		trademarckNameLabel = new JLabel("Marca:");
		trademarckNameLabel.setBounds(20, 550, 100, 20);
		trademarckNameLabel.setFont(new Font("Arial", Font.PLAIN, 20));
		add(trademarckNameLabel);
		trademarckNameLabel.setVisible(false);
		
		trademarckLabel = new JLabel();
		trademarckLabel.setBounds(180, 550, 250, 20);
		trademarckLabel.setFont(new Font("Arial", Font.PLAIN, 20));
		add(trademarckLabel);
		trademarckLabel.setVisible(false);
		
		serialNameLabel = new JLabel("Serial:");
		serialNameLabel.setBounds(20, 600, 100, 20);
		serialNameLabel.setFont(new Font("Arial", Font.PLAIN, 20));
		add(serialNameLabel);
		serialNameLabel.setVisible(false);

		serialLabel = new JLabel();
		serialLabel.setBounds(90, 600, 250, 20);
		serialLabel.setFont(new Font("Arial", Font.PLAIN, 20));
		add(serialLabel);
		serialLabel.setVisible(false);
	    
	    //Buscar por cedula

		queryArea = new JPanel();
		queryArea.setBounds(20, 300, 984, 390);
		queryArea.setFont(new Font("Arial", Font.PLAIN, 20));
		add(queryArea);
		queryArea.setVisible(false);
		
//		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd");
//		serialLabel.setText(sdf.format(datePickerFrom.getModel().getValue()));
        
	}
	
	public void ocultAll(){
		queryArea.setVisible(false);
		personLabel.setVisible(false);
		personNameLabel.setVisible(false);
		pictureLabel.setVisible(false);
		ticketLabel.setVisible(false);
		barcodeLabel.setVisible(false);
		deviceNameLabel.setVisible(false);
		deviceLabel.setVisible(false);
		trademarckNameLabel.setVisible(false);
		trademarckLabel.setVisible(false);
		serialNameLabel.setVisible(false);
		serialLabel.setVisible(false);
        labelFrom.setVisible(false);
        datePickerFrom.setVisible(false);
        labelTo.setVisible(false);
        datePickerTo.setVisible(false);
        labelTicket.setVisible(false);
        textTicket.setVisible(false);
        labelDocument.setVisible(false);
        textDocument.setVisible(false);
        buttonExport.setVisible(false);
        codeToFind = "";
        queryArea.removeAll();
    	personNameLabel.setText("");
		barcodeLabel.setText("");
		deviceLabel.setText("");
		trademarckLabel.setText("");
		serialLabel.setText("");
	}

	@Override
	public void actionPerformed(ActionEvent e) {
        if (e.getSource()==buttonSearch) {
        	if (findBy.equals("ticket")){
        		findByTicket();
		   }
        	if (findBy.equals("document")){
        		findByDocument();
        	}
        	if (findBy.equals("date")){
        		findByDate();
        	}
		}
        
        if (e.getSource()==buttonExport) {
        	JFileChooser chooser = new JFileChooser();
        	chooser.setAcceptAllFileFilterUsed(false);
        	chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);;
            int returnVal = chooser.showOpenDialog(getParent());
            if(returnVal == JFileChooser.APPROVE_OPTION) {
    			try {
    				filename = chooser.getSelectedFile().getAbsolutePath();
    				System.out.println(filename);
                	ExcelExporter exp = new ExcelExporter();
					exp.exportTable(tableExcel, new File(filename+File.separator+"results.xls"));
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
            }
		}
	}

	public void findByTicket () {
		ocultAll();
		codeToFind = textTicket.getText();
    	Code codigo = SqlConnection.consultarCodigo(codeToFind);
		
		if (codigo != null) {
			Image image = null;
			try {
				image = ImageIO.read(codigo.imagen);
			} catch (IOException e1) {
				e1.printStackTrace();
			}
			if (image != null) {
				pictureLabel.setIcon(new ImageIcon(image));
			}
	    	personLabel.setVisible(true);
	    	personNameLabel.setVisible(true);
			pictureLabel.setVisible(true);
			ticketLabel.setVisible(true);
			barcodeLabel.setVisible(true);
			deviceNameLabel.setVisible(true);
			deviceLabel.setVisible(true);
			trademarckNameLabel.setVisible(true);
			trademarckLabel.setVisible(true);
			serialNameLabel.setVisible(true);
			serialLabel.setVisible(true);
			personNameLabel.setText(codigo.nombreUsuario);
			barcodeLabel.setText(codigo.codigo);
			deviceLabel.setText(codigo.dispositivo);
			trademarckLabel.setText(codigo.marca);
			serialLabel.setText(codigo.serial);
		} else {
			JOptionPane.showMessageDialog(null, "CODIGO NO ENCONTRADO!!!");
		}
    	labelTicket.setVisible(true);
    	textTicket.setVisible(true);
	}
	
	public void findByDocument (){
		ocultAll();
		codeToFind = textDocument.getText();
		
		if (codeToFind != "") {
			try {
				JTable table;
				table = new JTable(SqlConnection.myModel(codeToFind));
				tableExcel = table;
				JScrollPane scroll = new JScrollPane(table);
				scroll.setPreferredSize(new Dimension(984,390));
				queryArea.add(scroll);
				queryArea.setVisible(true);
				
			} catch (Exception e1) {
				e1.printStackTrace();
			}
			buttonExport.setVisible(true);
			labelDocument.setVisible(true);
	        textDocument.setVisible(true);
		} 
		
	}
	
	public void findByDate (){
		ocultAll();
		if (codigoFrom != "" && codigoTo != "") {
			try {
				SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
				codigoFrom = sdf.format(datePickerFrom.getModel().getValue());
				codigoTo = sdf.format(datePickerTo.getModel().getValue());
				JTable table;
				table = new JTable(SqlConnection.myModelDate(codigoFrom, codigoTo));
				tableExcel = table;
				JScrollPane scroll = new JScrollPane(table);
				scroll.setPreferredSize(new Dimension(984,390));
				queryArea.add(scroll);
				queryArea.setVisible(true);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		buttonExport.setVisible(true);
		labelFrom.setVisible(true);
    	datePickerFrom.setVisible(true);
        labelTo.setVisible(true);
        datePickerTo.setVisible(true);
	}
	
	 public void windowClosing(WindowEvent e){
		 System.exit(0);
		 }
}